/**
 *  This file is part of PennerBot
 *
 *  Copyright (C) 2010 microkernel
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <wx/wx.h>

#include <gui/mainframe.hpp>
#include <gui/logindialog.hpp>

#include <core/configparser.hpp>

class PennerBot: public wxApp
{
    virtual bool OnInit();
};

IMPLEMENT_APP(PennerBot);

bool PennerBot::OnInit()
{
    curl_global_init(CURL_GLOBAL_ALL);

    wxImage::AddHandler(new wxPNGHandler);
    wxImage::AddHandler(new wxICOHandler);

    PBot::Core::ConfigParser parser;
    PBot::Core::Config* cfg = NULL;

    if(!parser.LoadFromFile("config.xml", &cfg) == PBot::Core::ConfigParser::ALL_CLEAR)
    {
        if(!cfg)
        {
            cfg = new PBot::Core::Config;
        }

        PBot::GUI::LoginDialog *dlg = new PBot::GUI::LoginDialog(cfg);

        bool correctData = false;

        while(!correctData)
        {
            if(dlg->ShowModal() == wxID_OK)
            {
                correctData = true;

                if(!dlg->GetUsername())
                {
                    wxMessageBox(wxT("Missing username"), wxT("PennerBot"), wxICON_ERROR);
                    correctData = false;
                    continue;
                }
                else
                {
                    cfg->username = dlg->GetUsername().mb_str(wxConvUTF8);
                }

                if(!dlg->GetPassword())
                {
                    wxMessageBox(wxT("Missing password"), wxT("PennerBot"), wxICON_ERROR);
                    correctData = false;
                    continue;
                }
                else
                {
                    cfg->password = dlg->GetPassword().mb_str(wxConvUTF8);
                }

                if(!dlg->GetUserAgent())
                {
                    wxMessageBox(wxT("Missing User-Agent"), wxT("PennerBot"), wxICON_ERROR);
                    correctData = false;
                    continue;
                }
                else
                {
                    cfg->userAgent = dlg->GetUserAgent().mb_str(wxConvUTF8);
                }

                cfg->game = dlg->GetGame();
                cfg->proxy = dlg->GetProxy().mb_str(wxConvUTF8);
                cfg->botname = "GUI";
            }
            else
            {
                dlg->Destroy();
                return true;
            }
        }

        dlg->Destroy();
        parser.SaveToFile("config.xml", cfg);
    }

    delete cfg;
    parser.LoadFromFile("config.xml", &cfg);


    PBot::GUI::MainFrame *frame = new PBot::GUI::MainFrame(cfg);

    frame->Show(true);
    SetTopWindow(frame);



    return true;
}


